const express = require('express')
const router = express.Router()
const humps = require('humps')
const db = require('../../database/db')

module.exports = (() => {
    // 包管理
    router.get('/packages', async (req, res, next) => {
        try {
            const doc = humps.camelizeKeys(await db.getPackages({ limit: req.query.limit, offset: req.query.offset, search: req.query.search }))
            const { total } = await db.getPackagesTotal(req.query.search)
            res.send({
                doc,
                total
            })
        } catch (err) {
            next(err)
        }
    })

    router.post('/addPackage', async (req, res, next) => {
        try {
            const promotion = await db.getPromotionByName(req.body.promotion)
            const center = await db.getCenterByName(req.body.center)
            const dept = await db.getDeptByName({ name: req.body.dept, center: req.body.center })
            const data = {
                promotion_id: promotion.id,
                time: req.body.time,
                cost: req.body.cost,
                amount: req.body.amount,
                center_id: center.id,
                dept_id: dept.id
            }
            const success = await db.addPackage(data)

            if (success) {
                res.json({
                    status: 1,
                    msg: 'ok'
                })
            }
        } catch (err) {
            next(err)
        }
    })

    router.put('/editPackage/:id', async (req, res, next) => {
        try {
            let existPackage = await db.getPackageById(req.params.id)
            if (!existPackage) {
                res.json({
                    status: 0,
                    msg: '该份包不存在！'
                })
                return
            }
            const promotion = await db.getPromotionByName(req.body.promotion)
            const center = await db.getCenterByName(req.body.center)
            const dept = await db.getDeptByName({ name: req.body.dept, center: req.body.center })
            const data = {
                id: req.params.id,
                promotion_id: promotion.id,
                time: req.body.time,
                cost: req.body.cost,
                amount: req.body.amount,
                center_id: center.id,
                dept_id: dept.id
            }
            const success = await db.updatePackage(data) // 更新包表数据

            if (success) {
                res.json({
                    status: 1,
                    msg: 'ok'
                })
            }
        } catch (err) {
            next(err)
        }
    })

    router.delete('/removePackage/:id', async (req, res, next) => {
        try {
            let existPackage = await db.getPackageById(req.params.id)

            if (!existPackage) {
                res.json({
                    status: 0,
                    msg: '该份包不存在！'
                })
                return
            }

            const success = await db.rmPackageById(req.params.id)// 删除包表数据

            if (success) {
                res.json({
                    status: 1,
                    msg: 'ok'
                })
            }
        } catch (err) {
            next(err)
        }
    })

    return router
})();